import pandas as pd
import numpy as np

def data_get():
    train = get_train()
    # data deal
    train = data_deal(train)
    train = Dummy_Variables(train)
    # data division
    x = train.drop('Loan_Status',axis=1)
    y = train.Loan_Status
    x = pd.get_dummies(x) 
    return x,y

def get_train():
    # data introduction
    train= pd.read_csv(r"train.csv")
    return train


def data_deal(train):
    # data lost
    train['Gender'].fillna(train['Gender'].mode()[0],inplace=True)
    train['Married'].fillna(train['Married'].mode()[0],inplace=True)
    train['Dependents'].fillna(train['Dependents'].mode()[0],inplace=True)
    train['Self_Employed'].fillna(train['Self_Employed'].mode()[0],inplace=True)
    train['Credit_History'].fillna(train['Credit_History'].mode()[0],inplace=True)

    # data fill
    train['Loan_Amount_Term'].fillna(train['Loan_Amount_Term'].mode()[0],inplace=True)
    train['LoanAmount'].fillna(train['LoanAmount'].median(),inplace=True)

    # 对数处理
    train['LoanAmount_log']=np.log(train['LoanAmount'])
    train['ApplicantIncome_log']=np.log(train['ApplicantIncome'])
    train['CoapplicantIncome_log']=np.log(train['CoapplicantIncome'])
    train['Loan_Amount_Term_log']=np.log(train['Loan_Amount_Term'])

    train = train.drop(['ApplicantIncome','CoapplicantIncome','LoanAmount','Loan_Amount_Term'],axis=1)
    train = train.replace(-np.inf, 0)    
    train = train.drop('Loan_ID',axis=1)
    return train

def Dummy_Variables(train):
    # Dummy Variables（虚拟变量），对于无序多分类变量
    #虚拟编码特征
    train['Dependents'].replace('3+',3, inplace=True)
    train['Loan_Status'].replace('N',0, inplace=True)
    train['Loan_Status'].replace('Y',1, inplace=True)
    return train

def data_tolist(df:pd.DataFrame):
    return [df.index.tolist(),df.values.tolist()]